﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace 剧情设计器2._0
{
    //公共静态类，负责全局通信
    public static class Common
    {
        //当前的文件名，打开、保存或另存为之后会发生变化，变成最近打开/保存的完整文件路径
        public static string filepath = "";

        //角色列表
        public static List<Character> characters = new List<Character>();
        //******故事类******////整个故事保存在这里
        public static Story story = new Story();
        
        //预设值
        //预设人物关系类型列表
        public static List<string> relationship_types = new List<string>();
        //预设动作类型列表
        public static List<string> action_types = new List<string>();
        //预设设定类型列表
        public static List<string> setting_types = new List<string>();

        //人物关系表，两个人物之间可以有多种关系
        public static Dictionary<string, Dictionary<string, List<string>>> relationshipTable = new Dictionary<string, Dictionary<string, List<string>>>();

        //上一次保存时的状态
        public static ValueTuple<int, int, int, int> last = new();

        //保存可视化窗口是否打开
        public static bool VisualViewOpened = false;
        public static VisualView vv = null;
        //通用方法：检查某个项是否在列表中，如果在，返回true，不在，返回false
        public static bool inList<T>(T item, List<T> list)
        {
            foreach(T t in list)
            {
                if (t.Equals(item)) return true;
            }
            return false;
        }

        //公共方法：判断角色是否存在与角色列表中
        public static bool inCharacters(string name)
        {
            foreach(Character c in characters)
            {
                string cname = c.name;
                if (name == cname) return true;
            }
            return false;
        }

        //更新主窗口的标题
        public static void updateTitle()
        {
            Program.mainform.Text = string.Format("剧情设计器2.0 - {0}", filepath);
        }

        public static int getCharactersHash(List<Character> cs)
        {
            int code = 0;
            foreach (Character c in cs)
                code += c.GetHashCode();
            return code;
        }

        public static int getStringLishHash(List<string> ls)
        {
            int code = 0;
            foreach (string s in ls)
                code += s.GetHashCode();
            return code;
        }

    public static T DeepCopy<T>(T obj)
    {
        object retval;
        using (MemoryStream ms = new MemoryStream())
        {
            XmlSerializer xml = new XmlSerializer(typeof(T));
            xml.Serialize(ms, obj);
            ms.Seek(0, SeekOrigin.Begin);
            retval = xml.Deserialize(ms);
            ms.Close();
        }
        return (T) retval;
    }

//这个类用于表示treeview节点的level的含义
public static class Level
        {
            public const int Story = 0;
            public const int Node = 1;
            public const int Plot = 2;
            public const int Event = 3;
            public const int Action = 4;
        }
    }
}
